mui.init({
	swipeBack: true
});
mui.plusReady(function() {
	
	var rdata = {};
	var fid = plus.webview.currentWebview().fid;
	var phone = plus.webview.currentWebview().phone;
	var header = plus.webview.currentWebview().head;
	var fname = plus.webview.currentWebview().fname;
	var state = app.getState();
	var uid = state.user_id;
	//console.log(phone);

	header = '../../static/images/addfriend_py.png';
	var rdata = {
		fid: fid,
		phone: phone,
		header: header,
		fname: fname,
		uid: uid
	};
	//Console.log(rdata);
	//console.log(JSON.stringify(rdata));
	var getUrl = apiurl + "user/getDetail/?phone=" + phone + "&user_id=" + uid + '&fid=' + fid;
	//console.log(getUrl);
	/*
	document.querySelector('.mui-media-object').setAttribute('src', "../" + header);
	document.querySelector('#name').innerHTML = chatName;
	document.querySelector('.mui-ellipsis').innerHTML = '昵称:' + chatName;
	*/
	mui.getJSON(getUrl, function(res) {
		//console.log(JSON.stringify(res.info));
		var uinfo = res.info || {};
		/*
		var avatar = uinfo.avatar;
		if(avatar && avatar.indexOf('upload') !== -1) {
			uinfo.avatar = serverurl + 'upload/avatar/' + uinfo.user_id + '.jpg';
		} else {
			uinfo.avatar = '../../static/images/addfriend_py.png';
		}*/
		var urls = JSON.parse(localStorage.getItem('avatarlist') || '{}');
		
		
		
		if(urls[fid]){
			var path = '_downloads/avatar/';
			//创建下载任务
			var loadUrl = dispimg + 'avatar/' + fid + '.jpg';
			
			var dtask = plus.downloader.createDownload(loadUrl, {
				filename: path
			}, function(d, status) {
				//console.log(status);
				if(status == 200) {
					//下载成功
					urls[fid] = plus.io.convertLocalFileSystemURL(d.filename);
					localStorage.setItem('avatarlist', JSON.stringify(urls));
				}
			}).start();
			uinfo.avatar = urls[fid];
			//delete urls[fid];
			//localStorage.setItem('avatarlist', JSON.stringify(urls));
			//uinfo.avatar = serverurl + 'upload/avatar/' + uinfo.user_id + '.jpg';
		}else{
			uinfo.avatar = app.avatar(fid, '../../static/images/1.png');
		}
		//console.log(fid);
		
		
		
		for(var i in uinfo.say) {
			uinfo.say[i].s_img = serverurl + uinfo.say[i].s_img;
		}
		uinfo.avatar = dispimg + 'avatar/' + fid + '.jpg';
		if(!CheckImgExists(uinfo.avatar)){
			uinfo.avatar = 'static/images/1.png';
		}
		
		console.log(JSON.stringify(uinfo));
		
		document.querySelector("#list-area").innerHTML = template('list-template', {
			"udata": uinfo,
			"rdata": rdata
		});
		if(uinfo) {

			var service_comfirm = plus.webview.getWebviewById("chatbox");
			service_comfirm && plus.webview.close(service_comfirm, "none");
			mui("#list-area").on('tap', '#sendmsg', function() {
				mui.openWindow({
					url: '/pages/chat/chat_index.html',
					id: 'chatbox',

					show: {
						autoShow: true,
						aniShow: 'slide-in-right',
						duration: 100
					},
					waiting: {
						autoShow: true,
						title: 'waiting...'
					},
					extras: {
						chatHeader: uinfo.avatar,
						chatTitle: uinfo.name,
						roomId: uinfo['user_id'],
						roomType: 1
					}
				});
			});
			mui("#list-area").on('tap', '#userdesc', function() {
				var btnArray = ['确定', '取消'];
				mui.prompt('好友的备注名字', uinfo.userdesc, '', btnArray, function(e) {
					if(e.index == 0) {
						if(e.value && e.value.length > 0) {
							sysphone.setOneFriend(uinfo['user_id'], 'userdesc', e.value);
							document.querySelector('#userdesc .mui-media-body').innerHTML = '备注:' + e.value;
						}
					} else {
						//info.innerText = '你点了取消按钮';
					}
				}, 'div');
			});
			/*
			mui("#list-area").on('tap', '#sendvideo', function() {
				var service_comfirm = plus.webview.getWebviewById("video_wait.html");
				service_comfirm && plus.webview.close(service_comfirm, "none");
				var mainid = localStorage.getItem('mainpage');
				var mainWin = plus.webview.getWebviewById(mainid);
				mui.fire(mainWin, 'addvideo', {
					room_id: uinfo.user_id
				});
				mui.openWindow({
					url: "../../pages/chat/video_wait.html",
					id: "video_wait.html",
					extras: {
						'toid': uinfo.user_id,
						'toname': uinfo.name
					}
				});
			});*/
		}

	});

	mui("#list-area").on('tap', '#sendtel', function() {
		if(mui.os.plus) {
			plus.device.dial(phone);
		} else {
			location.href = 'tel:' + phone;
		}
	});
	mui("#list-area").on('tap', '#sendsms', function() {
		var msg = plus.messaging.createMessage(plus.messaging.TYPE_SMS);
		msg.to = [phone];
		msg.body = '';
		plus.messaging.sendMessage(msg);
	});
	mui("#list-area").on('tap', '#sendreg', function() {
		var msg = plus.messaging.createMessage(plus.messaging.TYPE_SMS);
		msg.to = [phone];
		msg.body = '我是' + state.name + ',快来注册GAT CHAT吧，注册地址：' + regAppurl + '?fid=' + uid + '&phone=' + phone + '&name=' + fname;
		plus.messaging.sendMessage(msg);
	});
	mui("#list-area").on('tap', '#sendadd', function() {
		var postdata = {
			fid: fid,
			user_id: uid
		};
		//console.log(uid);
		mui.post(apiurl + 'user/addfriends', postdata, function(res) {
			console.log(res.status);
			if(res.status > 0) {
				mui.alert("请等待好友通过！");
				//that.setAttribute('class', 'add');
				//that.innerHTML = '申请中';
			} else {
				mui.alert(res.msg);
			}
		});
	});
	
	mui("#list-area").on('tap', '#delfriend', function() {
		var postdata = {
			fid: fid,
			user_id: uid
		};
		var back_id = plus.webview.currentWebview().back_id;
		var btnArray = ['确定', '取消'];
			mui.confirm('确定要删除朋友'+document.getElementById('friend_name').value+'?', '提示', btnArray, function(e) {
					if (e.index == 0) {
						mui.post(apiurl + 'user/delfriends', postdata, function(res) {
							console.log(res.status);
							if(res.status > 0) {
								mui.alert("删除成功！");
				                //location.href='../../main.html';
				                if(back_id=='friendlist'){
				                	var findwin = plus.webview.getWebviewById("pages/contacts/friendlist.html");
				                    mui.fire(findwin, 'refreshList');
				                }
				                if(back_id=='newfriend'){
				                	var findwin = plus.webview.getWebviewById("pages/contacts/newfriend.html");
				                    mui.fire(findwin, 'refreshList');
				                }
				
				                mui.back();
							} else {
								mui.alert(res.msg);
							}
						});
		
					} else {
						return ;
					}
			})
	});
	
	
	mui("#list-area").on('tap', '#photo', function() {
		mui.openWindow({
			url: '/pages/friends/profile.html',
			id: '/pages/friends/profile.html',
			extras: {
				u_id: document.querySelector('#photo').getAttribute('uid'),
				head_img: serverurl + document.querySelector('#photo').getAttribute('avatar'),
				phone: document.querySelector('#photo').getAttribute('phone'),
				name: document.querySelector('#photo').getAttribute('name')
			}
		})
	});

	var pop = new popover('#pop');

	mui("#list-area").on('click', 'img', function() {

		var src = this.getAttribute('src');
		document.querySelector('#pop img').setAttribute('src', dispimg + 'avatar/' + fid + '.jpg');
		document.querySelector('#pop').style.display = 'block';
		document.querySelector("#list-area").style.display = 'none';
		document.querySelector("button").style.display = 'none';
		document.querySelector(".mui-table-view").style.display = 'none';

		//app.avatar(fid);
	});

	mui("#pop").on('click', 'img', function() {
		document.querySelector('#pop').style.display = 'none';
		document.querySelector("#list-area").style.display = 'block';
		document.querySelector("button").style.display = 'block';
		document.querySelector(".mui-table-view").style.display = 'block';
	});
});

/*
document.querySelector('#openPopover').addEventListener('click', function() {
	// 弹出系统选择按钮框
	plus.nativeUI.actionSheet({
		title: "设置备注及标签",
		cancel: "取消",
		buttons: [{
			title: "标为星标朋友"
		}, {
			title: "发送该名片"
		}, {
			title: "投诉"
		}, {
			title: "加入黑名单"
		}, {
			title: "删除"
		}, {
			title: "添加到桌面"
		}]
	}, function(e) {
		console.log("User pressed: " + e.index);
	});
});*/
function CheckImgExists(imgurl) {  
    var ImgObj = new Image(); //判断图片是否存在  
    ImgObj.src = imgurl;  
    //没有图片，则返回-1  
    if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {  
        return true;  
    } else {  
        return false;
    }  
} 